using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Squares
{
    class Program
    {
        static void Main(string[] args) {
            Map map = ScanFile("map2.txt");
            //Alan! Map2 isn't in the SVN! See if you can add it.
            Map map2 = ScanFile("map2.txt");     // Map with editing (what is behind
                                                //      the train, etc)

            MoveTrain(map, map2);

            Console.ReadLine();
        }

        static Map ScanFile(string file)
        {
            //----------------------[Discussion of Features]-----------------------
            //= is track entry
            //# is regular track
            //1 is grass[no train :(]
            //n is train car

            string[] lines;
            Map map;
            bool IsEqualLength = true;

            //Load file into memory
            using (StreamReader sr = new StreamReader(file)) lines = sr.ReadToEnd().Split('\n');

            //check all lines for the same length as the first line
            foreach (string line in lines) IsEqualLength &= line.Length == lines[0].Length;

            map = new Map(lines.Length, lines[0].Length);

            if (!IsEqualLength) {
                //Some sort of exception.
            }

            for (int i = 0; i < lines.Length; i++)
            {
                for (int j = 0; j < lines[i].Length; j++)
                {
                    //The character at location i, j is that of the character at j in a string contained
                    //in lines at i
                    map[i, j] = lines[i][j];
                }
            }

            return map;
        }
        
        static void MoveTrain(Map map, Map map2) //Please link to main loop - Alan Cheng
        {
            //Hierarchy of directions (for now) N -> W -> S -> E
    
            int X1 = 0, Y1 = 0;    // Current Map Cell
            
            
            //-----------[Find the Starting Point]--------------
            
            for(int i = 0; i <= map.GetUpperBound(0); i++)
            {
                for(int j = 0; j <= map.GetUpperBound(1); j++)
                {
                    if(map[i,j] == '=')
                    {
                        X1 = i;
                        Y1 = j;
                    }
                }
            }

            //Console.WriteLine(X1 + " " + Y1);

            //Write out map
            for (int i = 0; i <= map.GetUpperBound(0); i++) {
                for (int j = 0; j <= map.GetUpperBound(1); j++) {
                    Console.Write(map[i, j]);
                }
                Console.WriteLine();
            }

            Console.SetCursorPosition(Y1, X1);
            Console.Write("N");

            //----------------[Move the Train]------------------
            
            while(true)//( X1 >= map.GetUpperBound(0) ) && ( Y1 >= map.GetUpperBound(1)))
            {
                Console.SetCursorPosition(X1, Y1);
                //Console.Write('#');
                //--Decision Making--
                if ((Y1 - 1) >= 0)
                {
                    if ((map2[X1, Y1 - 1] == '#'))
                    {
                        Y1 = Y1 - 1;
                        map2[X1, Y1] = 'N';
                        Console.SetCursorPosition(Y1, X1);
                        Console.Write("N");
                    }
                }
                if ((X1 + 1) <= 9)
                {
                    if ((map2[X1 + 1, Y1] == '#'))
                    {
                        X1 = X1 + 1;
                        map2[X1, Y1] = 'N';
                        Console.SetCursorPosition(Y1, X1);
                        Console.Write("N");
                    }
                }
                if ((Y1 + 1) <= 10)
                {
                    if ((map2[X1, Y1 + 1] == '#'))
                    {
                        Y1 = Y1 + 1;
                        map2[X1, Y1] = 'N';
                        Console.SetCursorPosition(Y1, X1);
                        Console.Write("N");

                    }
                }
                if ((X1 - 1) >= 0)
                {
                    if ((map2[X1 - 1, Y1] == '#'))
                    {
                        X1 = X1 - 1;
                        map2[X1, Y1] = 'N';
                        Console.SetCursorPosition(Y1, X1);
                        Console.Write("N");

                    }
                }

                Console.ReadLine();
            }
        }
    }
}
